/*
 * Copyright 2011 Philippe Blanc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package me.l1k3.fx.client;

import me.l1k3.fx.client.channel.ClipChannel;
import me.l1k3.fx.client.channel.inter.DimensionConstant;
import me.l1k3.fx.client.channel.inter.DimensionConstant.AnchorHorizontal;
import me.l1k3.fx.client.channel.inter.DimensionConstant.AnchorVertical;
import me.l1k3.fx.client.channel.inter.DimensionConstant.Direction;
import me.l1k3.fx.client.impl.FXShowAndHideAbstract;
import me.l1k3.fx.client.inter.FX;

import com.google.gwt.dom.client.Element;

public class FXClip extends FXShowAndHideAbstract {
    private ClipChannel clipChannel;
    
	public FXClip() {
		this(Direction.VERTICAL, AnchorHorizontal.CENTER, AnchorVertical.TOP);
	}
	
	public FXClip(Direction direction) {
        this(direction, AnchorHorizontal.LEFT, AnchorVertical.TOP);
    }
	
	public FXClip(Direction direction, AnchorHorizontal anchorHorizontal, AnchorVertical anchorVertical) {
	    clipChannel = ClipChannel.clip(getType(), direction, anchorHorizontal, anchorVertical);
    }
	
	//- - - factory
	
    public final static FXClip clip(Element element) {
        FXClip clip = new FXClip(Direction.BOTH, AnchorHorizontal.CENTER, AnchorVertical.CENTER);
        clip.init(element);
        
        return clip;
    }
    
    public final static FXClip vertical(Element element) {
        FXClip clip = new FXClip(Direction.VERTICAL, AnchorHorizontal.CENTER, AnchorVertical.TOP);
        clip.init(element);
        
        return clip;    
    }
    
    public final static FXClip horizontal(Element element) {
        FXClip clip = new FXClip(Direction.HORIZONTAL, AnchorHorizontal.LEFT, AnchorVertical.CENTER);
        clip.init(element);
        
        return clip;    
    }

    //- - -
    
    public void clip(DimensionConstant.Direction Direction, DimensionConstant.AnchorHorizontal AnchorHorizontal, DimensionConstant.AnchorVertical AnchorVertical) {
        clipChannel.set(Direction, AnchorHorizontal, AnchorVertical);
    }
    
	//- - -
    
    @Override
    public boolean isVisible() {
        return clipChannel.isForward();
    }
    
    //- - -
    
    /**
     * @exclude
     */
    @Override
    public FX init(Element element) {
        super.init(element);
        clipChannel.init(context);
        validateChannel(clipChannel);
        
        return this;
    }
	
    /**
     * @exclude
     */
    @Override
    public FX toggle() {
        clipChannel.toggle(context);
        
        return this;
    }

    /**
     * @exclude
     */
    @Override
    public void onStart() {
        super.onStart();
        clipChannel.start(context);
    }
    
    /**
     * @exclude
     */
    @Override
    public void onUpdate(double progress) {
        context.setProgress(progress);
        clipChannel.update(context);
    }   
    
    /**
     * @exclude
     */
    @Override
    public void onComplete() {
        clipChannel.complete(context);
        super.onComplete();
    }
}
